<!--
 * @description: 第三方系统登录成功后回调后端接口，后端接口重定向到该页面，
 * 并从url中获取token和用户信息后，登录成功，跳转首页
 * @author: huangpeng
 * @date: 2025年01月08日 15:14
 * @Copyright:  2022-2025
!-->
<template>
  <div style="padding: 20px;">
    <h1>登录中...</h1>
  </div>
</template>
<script lang="ts">
import {Component, Vue} from "vue-property-decorator";
import {setExpiresIn, setToken, setUsername} from "@/utils/auth";

@Component({})
export default class thirdLogin extends Vue {

  private created() {
    //获取到token 并封装到store中用于全程携带
    let data = this.getToken();
    if (![null, '', undefined].includes(data.access_token)){
      setToken(data.access_token)
      this.$store.commit('SET_TOKEN', data.access_token)
      setExpiresIn(data.expires_in)
      this.$store.commit('SET_EXPIRES_IN', data.expires_in)
      setUsername(data.username);
      this.$message.success("登录成功，即将跳转首页");
      setTimeout(() => {
        this.$router.push({ path: "/index?userId=" + data.userId }).catch(()=>{});
      }, 2000);
    } else {
      this.$message.error("当前第三方账号未绑定本系统账户,请重新登录！");
      setTimeout(() => {
        this.$router.push({ path: "/login" }).catch(()=>{});
      }, 2000);
    }
  }

  /** 解析url中的token和用户名参数 */
  private getToken(): any {
    const url = window.document.location.href.toString();//获得当前url地址并转换成字符串
    console.log(url)
    let params = url.split("?");//以？来分割字符串，将？前后的值存到一个数组中
    params = params[1].split("&");
    let result: any = {};
    for (let param of params) {
      const paramItem = param.split("=");
      result[paramItem[0]] = paramItem[1];
    }
    return result;
  }
}
</script>
<style scoped lang="scss">
</style>
